<!--  -->
<template>
  <div class="good_detail">
    <!-- 头部 -->
    <NavBar class="home_navbar">
      <div @click="goBack" slot="left">
        <i class="iconfont icon-fanhui"></i>
      </div>
      <div slot="center">商品详情</div>
    </NavBar>

    <!-- 轮播图 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#333">
      <van-swipe-item
        @click="handlePrevew"
        v-for="(item, index) in goodInfo.pics"
        :key="index"
      >
        <van-image contain :src="item.pics_mid" alt="" />
      </van-swipe-item>
    </van-swipe>
    <div class="goods_price">￥{{ goodInfo.goods_price }}</div>

    <div class="goods_name_row">
      <div class="goods_name">{{ goodInfo.goods_name }}</div>
      <div @click="handleCollect" class="goods_collect">
        <span
          v-if="isCollect"
          bindtap="handleCollect"
          class="iconfont icon-shoucang"
        ></span>
        <span
          v-else
          bindtap="handleCollect"
          class="iconfont icon-shoucang1"
        ></span>
        <div class="collect_text">收藏</div>
      </div>
    </div>

    <div class="goods_info">
      <div class="goods_info_title">图文详情</div>
      <div class="goods_info_content">
        <div v-html="goodInfo.goods_introduce"></div>
      </div>
    </div>
    <div class="cart">
      <van-goods-action>
        <van-goods-action-icon icon="chat-o" text="客服" @click="onClickIcon" />
        <van-goods-action-icon @click="toCart" icon="cart-o" text="购物车" />
        <van-goods-action-icon
          icon="share-o"
          text="分享"
          @click="onClickIcon"
        />
        <van-share-sheet
          v-model="showShare"
          title="立即分享给好友"
          :options="options"
          @select="onSelect"
        />
        <van-goods-action-button
          type="danger"
          text="加入购物车"
          @click="onClickButton"
        />
        <van-goods-action-button type="danger" @click="isShow = true" text="立即购买" />
      </van-goods-action>
      <van-dialog v-model="isShow" title="结算中" show-cancel-button>
        <img src="@/assets/img/mine.jpg" />
      </van-dialog>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { Toast } from "vant";
import { ImagePreview } from "vant";
Vue.use(Toast);
import NavBar from "@/components/common/navBar/NavBar.vue";
import { getGoodsDetail } from "@/util/category";
export default {
  name: "GoodDetail",
  components: {
    NavBar,
  },
  data() {
    return {
      currentId: this.$route.params.cid,
      goodInfo: [],
      isCollect: false,
      showShare: false,
      isShow:false,
      options: [
        { name: "微信", icon: "wechat" },
        { name: "微博", icon: "weibo" },
        { name: "复制链接", icon: "link" },
        { name: "分享海报", icon: "poster" },
        { name: "二维码", icon: "qrcode" },
      ],
    };
  },
  created() {
    this.getGoodsDetail(this.currentId);
  },
  mounted() {
    this.$router.afterEach(() => {
      window.scrollTo(0, 0);
    });
  },
  watch: {
    $route(to) {
      if (to.params.cid) {
        this.currentId = to.params.cid;
        this.getGoodsDetail(this.currentId);
      }

      //   console.log(to);
    },
  },
  methods: {
    async getGoodsDetail(id) {
      let { data } = await getGoodsDetail(id);
      // console.log(data);
      this.goodInfo = data.message;
    },
    goBack() {
      this.$router.go(-1);
    },
    handleCollect() {
      this.isCollect = !this.isCollect;
      if (this.isCollect === true) {
        Toast.success("收藏成功");
      } else {
        Toast.success("取消成功");
      }
    },
    onClickIcon() {
      this.showShare = true;
    },
    onClickButton() {
      let cart = JSON.parse(localStorage.getItem("cart")) || [];
      let index = cart.findIndex((v) => v.goods_id === this.goodInfo.goods_id);
      if (index === -1) {
        this.goodInfo.num = 1;
        this.goodInfo.checked = true;
        cart.push(this.goodInfo);
      } else {
        cart[index].num++;
      }
      Toast.success({
        message: "加入成功",
        forbidClick: true,
      });
      localStorage.setItem("cart", JSON.stringify(cart));
    },
    onSelect(option) {
      Toast(option.name);
      this.showShare = false;
    },
    handlePrevew() {
      let urls = this.goodInfo.pics.map((v) => v.pics_mid);
      // console.log(urls);
      ImagePreview({
        images: urls,
        startPosition: 0,
        showIndicators: true,
      });
    },
    toCart() {
      this.$router.push("/cart");
    },
  },
};
</script>

<style scoped lang='less'>
.good_detail {
  margin-bottom: 60px;
}
.home_navbar {
  background-color: #eb4450;
  color: #fff;
  font-weight: 600;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9;
}
.my-swipe {
  margin-top: 44px;
}
.my-swipe .van-swipe-item {
  color: #eee;
  font-size: 20px;
  text-align: center;
  height: 220px;
  line-height: 220px;
  .van-image {
    width: 60%;
  }
}
.goods_price {
  padding: 8px;
  font-size: 16px;
  font-weight: 600;
  color: red;
}
.goods_name_row {
  display: flex;
  border-top: 5px solid #dedede;
  border-bottom: 5px solid #dedede;
  padding: 5px 0;

  .goods_name {
    flex: 5;
    color: #000;
    font-size: 14px;
    padding: 0 5px;

    display: -webkit-box;
    overflow: hidden;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .goods_collect {
    border-left: 1px solid #000;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 0 4px;

    .iconfont {
      font-size: 22px;
      color: red;
    }

    .collect_text {
      font-size: 13px;
    }
  }
}
.goods_info {
  .goods_info_title {
    font-size: 16px;
    font-weight: 600;
    color: red;
    padding: 10px;
  }
}
.cart {
  position: relative;
  z-index: 666;
}
.van-dialog{
    img{
        width: 100%;
    }
}
</style>